<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-HTTP.
 *
 * PHP-HTTP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-HTTP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-HTTP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-http
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */
 
class Cookie {


    // ----------------------------------------------------- Instance Variables


    private $name;
    private $value;
    private $expire;
    private $path;
    private $domain;
    private $secure;
    private $httponly;


    // ----------------------------------------------------------- Constructors


    public function __construct($name, $value=null, $expire = 0, $path=null,
        $domain=null, $secure=false, $httponly=false) {

        $this->name = $name;
        $this->value = $value;
        $this->expire = $expire;
        $this->path = $path;
        $this->domain = $domain;
        $this->secure = $secure;
        $this->httponly = $httponly;
    }


    // ------------------------------------------------------------- Properties


    /**
     * Return the name of the cookie.
     * 'cookiename' is called as $_COOKIE['cookiename']
     */
    public function getName() {
        return $this->name;
    }


    /**
     * Set the name of the cookie.
     * 'cookiename' is called as $_COOKIE['cookiename']
     *
     * @param name The name of the cookie
     */
    public function setName($name) {
        $this->name = $name;
    }



    /**
     * Return the value of the cookie.
     * This value is stored on the clients computer;
     * do not store sensitive information.
     */
    public function getValue() {
        return $this->value;
    }


    /**
     * Set the value of the cookie.
     * This value is stored on the clients computer;
     * do not store sensitive information.
     *
     * @param value The value of the cookie.
     */
    public function setValue($value) {
        $this->value = $value;
    }


    /**
     * Return the time the cookie expires. This is a Unix timestamp so
     * is in number of seconds since the epoch. In other words,
     * you'll most likely set this with the time() function plus
     * the number of seconds before you want it to expire. Or you
     * might use mktime().
     *
     * time()+60*60*24*30 will set the cookie to expire in 30 days.
     * If set to 0, or omitted, the cookie will expire at the end of
     * the session (when the browser closes).
     */
    public function getExpire() {
        return $this->expire;
    }


    /**
     * Set the time the cookie expires. This is a Unix timestamp so 
     * is in number of seconds since the epoch. In other words,
     * you'll most likely set this with the time() function plus
     * the number of seconds before you want it to expire. Or you
     * might use mktime().
     *
     * time()+60*60*24*30 will set the cookie to expire in 30 days.
     * If set to 0, or omitted, the cookie will expire at the end of
     * the session (when the browser closes).
     *
     * @param expire The time the cookie expires 
     */
    public function setExpire($expire) {
        $this->expire = $expire;
    }


    /**
     * Return the path on the server in which the cookie will be
     * available on.
     *
     * If set to '/', the cookie will be available within the entire
     * domain. If set to '/foo/', the cookie will only be available
     * within the /foo/ directory and all sub-directories such as
     * /foo/bar/ of domain. The default value is the current directory
     * that the cookie is being set in.
     */
    public function getPath() {
        return $this->path;
    }


    /**
     * Set the path on the server in which the cookie will be
     * available on.
     *
     * If set to '/', the cookie will be available within the entire
     * domain. If set to '/foo/', the cookie will only be available
     * within the /foo/ directory and all sub-directories such as
     * /foo/bar/ of domain. The default value is the current directory
     * that the cookie is being set in.
     *
     * @param path the path on the server in which the cookie will be
     * available on
     */
    public function setPath($path) {
        $this->path = $path;
    }


    /**
     * Return the domain that the cookie is available.
     *
     * To make the cookie available on all subdomains of example.com
     * then you'd set it to '.example.com'. The . is not required but
     * makes it compatible with more browsers. Setting it to www.example.com
     * will make the cookie only available in the www  subdomain.
     */
    public function getDomain() {
        return $this->domain;
    }


    /**
     * Set the domain that the cookie is available.
     *
     * To make the cookie available on all subdomains of example.com
     * then you'd set it to '.example.com'. The . is not required but
     * makes it compatible with more browsers. Setting it to www.example.com
     * will make the cookie only available in the www  subdomain.
     *
     * @param domain The domain that the cookie is available.
     */
    public function setDomain($domain) {
        $this->domain = $domain;
    }


    /**
     * Indicates that the cookie should only be transmitted over a secure
     * HTTPS connection from the client. When set to TRUE, the cookie will
     * only be set if a secure connection exists. The default is FALSE. On
     * the server-side, it's on the programmer to send this kind of cookie
     * only on secure connection (e.g. with respect to $_SERVER["HTTPS"]).
     */
    public function getSecure() {
        return $this->secure;
    }


    /**
     * Indicates that the cookie should only be transmitted over a secure
     * HTTPS connection from the client. When set to TRUE, the cookie will
     * only be set if a secure connection exists. The default is FALSE. On
     * the server-side, it's on the programmer to send this kind of cookie
     * only on secure connection (e.g. with respect to $_SERVER["HTTPS"]).
     *
     * @param secure Boolean transmit over a secure HTTPS connection
     */
    public function setSecure($secure) {
        $this->secure = $secure;
    }


    /**
     * When TRUE the cookie will be made accessible only through the HTTP protocol.
     * This means that the cookie won't be accessible by scripting languages, such
     * as JavaScript. This setting can effectly help to reduce identity theft
     * through XSS attacks (although it is not supported by all browsers).
     * Added in PHP 5.2.0.
     */
    public function getHttponly() {
        return $this->httponly;
    }


    /**
     * When TRUE the cookie will be made accessible only through the HTTP protocol.
     * This means that the cookie won't be accessible by scripting languages, such
     * as JavaScript. This setting can effectly help to reduce identity theft
     * through XSS attacks (although it is not supported by all browsers).
     * Added in PHP 5.2.0.
     *
     * @param httponly Boolean make accessible only through the HTTP protocol
     */
    public function setHttponly($httponly) {
        $this->httponly = $httponly;
    }


    public function __toString() {

        $sb = "[Cookie";
        $sb .= ",\n\tname=".$this->getName();
        $sb .= ",\n\tpath=".$this->getPath();
        $sb .= ",\n\texpire=".$this->getExpire();
        $sb .= ",\n\tvalue=".$this->getValue();
        $sb .= ",\n\tsecure=".$this->getSecure();
        $sb .= ",\n\thttponly=".$this->getHttponly();
        $sb .= "\n]";

        return $sb;

    }

}